$numOfSeries = 10;
$lengthOfSeries = 100;
$occupiedRatio = 0.5;
$correlated = 0.5;
$positive = 0;

@noise;
for($m=0;$m<$numOfSeries;$m++){	
	$noise[$m] = int(rand(8))-100;  #noise floor is [-92,-100].
}
@sample;
for($i=0;$i<$lengthOfSeries;$i++){
	for($j=0;$j<$numOfSeries;$j++){
		$sample[$j][$i]=0;
	}
}
$i = 0;
#$occupiedRatio = int(rand(10));
#$occupiedRatio /= 10;
$occupied = 0;
$fileName="correlatedSeriesL$numOfSeries"."occp$occupiedRatio.log";

for($j=0;$j<$lengthOfSeries;$j++) {

	for($i=0;$i<$numOfSeries;$i++){
		$temp = -100;
		
		if($i==0){
			if(rand()>$occupiedRatio){
				$occupied = 0;
				$temp = $noise[$i] + int(rand(4))-2;  # give a [-2,2] fluction to noise floor.
			}
			else{
				$occupied = 1;
				$temp = 0 - int(rand(80));
			}
		}
		
		#positive correlation
		if($positive==1 && $i!=0 && rand()>$correlated){
			if(rand()>$occupiedRatio){
				$temp = $noise[$i] + int(rand(4))-2;  # give a [-2,2] fluction to noise floor.
			}
			else{ 
				$temp = 0 - int(rand(80));
			}
		}
		if($positive==1 && $i!=0 && rand()<=$correlated){
			if($occupied==0){
				$temp = $noise[$i] + int(rand(4))-2;  # give a [-2,2] fluction to noise floor.
			}
			else{
				$temp = $sample[0][$j] + int(rand(20))-10; 
				if($temp<-90){$temp=-89;}
			}
		}
		
		#negative correlation
		if($positive==0 && $i!=0 && rand()>$correlated){
			if(rand()>$occupiedRatio){
				$temp = $noise[$i] + int(rand(4))-2;  # give a [-2,2] fluction to noise floor.
			}
			else{ 
				$temp = 0 - int(rand(80));
			}
		}
		if($positive==0 && $i!=0 && rand()<=$correlated){
			if($occupied==0){
				$temp = $sample[0][$j] + int(rand(20))-10; 
				if($temp<-90){$temp=-89;}
			}
			else{
				$temp = $noise[$i] + int(rand(4))-2;  # give a [-2,2] fluction to noise floor.
			}
		}
		if($temp>0){
			$temp = 0;
		}
		$sample[$i][$j]=$temp;
		
	}
}



open fout, ">.\\randomDistance\\$fileName" or die "can not open file .\\randomDistance\\$fileName\n";
for($i=0;$i<$lengthOfSeries+1;$i++){
	for($j=0;$j<$numOfSeries;$j++){
		print fout "$sample[$j][$i] ";
	}
	print fout "\n";
}
close fout;

#dtw
@distance;
@cost;
$pathlength = 0;
$windowSize = 2;

for($i=0;$i<$lengthOfSeries;$i++){
	for($j=0;$j<$lengthOfSeries;$j++){
		$distance[$i][$j]=100000;
	}
}
$distance[0][0]=0;
for($k=1;$k<$numOfSeries;$k++){
	for($i=1;$i<$lengthOfSeries;$i++){
		for($j=max(1,$i-$windowSize);$j<min($lengthOfSeries,$i+$windowSize);$j++){
		#for($j=1;$j<$lengthOfSeries;$j++){
			$distance[$i][$j]= abs($sample[0][$i]-$sample[$k][$j]) + min(min($distance[$i][$j-1], $distance[$i-1][$j]), $distance[$i-1][$j-1]);
			#print "$distance[$i][$j] ";
		}
		#print "\n";
	}

	print ("$k "."$distance[$lengthOfSeries-1][$lengthOfSeries-1] "."$distance[$lengthOfSeries][$lengthOfSeries] \n");
}

$sum=0;
for($m=2;$m<=13;$m++){
	$sum += 2*(13-$m+1)*($m-1);
}
$sum= $sum/($m*$m);
#print "13 $sum\n";
	
$sum=0;
for($m=2;$m<=90;$m++){
	$sum += 2*(90-$m+1)*($m-1);
}
$sum= $sum/($m*$m);
#print "90 $sum\n";
	
print "Correlated occupiedRatio: $occupiedRatio\n";
sub min{
	($a,$b)=@_;
	
	if($a>$b){
		return $b;
	}
	else{
		return $a;
	}
}

sub max{
	($a,$b)=@_;
	
	if($a>$b){
		return $a;
	}
	else{
		return $b;
	}
}
